package com.company;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;

public class NonReentrantLockTest {
    final static NonReentrantLock lock = new NonReentrantLock();
    final static Condition notFull = lock.newCondition();
    final static Condition notEmpty = lock.newCondition();
    final static Queue<String> queue = new LinkedBlockingQueue<String>();
    final static  int queueSize = 10;
    public static void main(String[] args) {

        Thread producer = new Thread(new  Runnable() {
            public void run() {
                //获取独占锁
                lock.lock();
                try{
                    //(1)如果队列满了，则等待
                    while(queue.size() == queueSize){
                        System.out.println( "producer=阻塞start" );
                        notEmpty.await(); ;
                        System.out.println( "producer=阻塞end" );
                    }
                    //（2）添加元素到队列
                    queue.add("ele");
                    System.out.println( "add=ele");
                    //（3）唤醒消费线程
                    notFull.signalAll();
                }catch(Exception e){
                    e.printStackTrace();
                }finally {
                    //释放锁
                    lock.unlock();
                }
            }
        });
        Thread consumer = new Thread(new  Runnable() {
            public void run() {
                //获取独占锁
                lock.lock();
                try{
                    //队列空，则等待
                    while(0 == queue.size() ){
                        System.out.println( "reduce=阻塞start" );
                        notFull.await(); ;
                        System.out.println( "reduce=阻塞end" );
                    }
                    //消费一个元素
                    String ele = queue.poll();
                    System.out.println( "reduce="+ele );
                    //唤醒生产线程
                    notEmpty.signalAll();
                }catch(Exception e){
                    e.printStackTrace();
                }finally {
                    //释放锁
                    lock.unlock();
                }
            }
        });
        //启动线程

        consumer.start();
        producer.start();
    }
}
